package com.taiji.dianthus.dao.sys;


import com.taiji.dianthus.domain.sys.DSysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

/**
 * @ClassName UserRepository
 * @Description 用户相关
 * @Author H.M
 * @Date 2019/9/9
 */
@Repository
public interface UserRepository extends JpaRepository<DSysUser, String>, JpaSpecificationExecutor<DSysUser> {

    /**
     * 获取用户名
     *
     * @param userCode
     * @return
     */
    @Query("select u.name from DSysUser u where u.id=:userCode and u.enableFlag=1")
    String getNameByCode(@Param("userCode") String userCode);

    /**
     * 逻辑删除
     *
     * @param id
     */
    @Modifying
    @Transactional
    @Query("update DSysUser p set p.enableFlag = 0  where p.id=:id")
    void updateFlag(@Param("id") String id);

    /**
     * 根据登录名查找
     *
     * @param loginName
     * @return
     */
    @Query("select u from DSysUser u where u.userLoginName =:loginName and u.enableFlag=1")
    DSysUser findByLoginName(@Param("loginName") String loginName);

    /**
     * 根据ID查找
     *
     * @param id
     * @return
     */
    @Query("select u from DSysUser u where u.id =:id and u.enableFlag=1")
    DSysUser getById(@Param("id") String id);

    /**
     * 查询
     *
     * @param orgId
     * @return
     */
    @Query("select count(*) from DSysUser u where u.orgId =:orgId and u.enableFlag=1")
    Integer countByOrgId(@Param("orgId") String orgId);

    @Query("select u from DSysUser u where u.orgId =:orgId and u.enableFlag=1")
    List<DSysUser> listUserByOrgId(@Param("orgId") String orgId);

    /**
     * 查询所有业务人员集合
     *
     * @return
     */
    @Query(value = "select * from d_sys_user  t where  t.user_login_name != 'admin' and  t.enable_flag = 1 ",
            nativeQuery = true)
    List<Map<String, Object>> listAllBusinessUser();

    /**
     * 根据登录名查询数量
     *
     * @param loginName
     * @return
     */
    @Query(value = "select count(*) from DSysUser u where u.userLoginName =:loginName and  u.enableFlag = 1")
    Integer countByLoginName(String loginName);


    /**
     * 查询所有有组织用户
     *
     * @return
     */
    @Query(value = "SELECT t FROM DSysUser t WHERE  t.orgId is not null and t.orgId <>''  and t.enableFlag = 1 ")
    List<DSysUser> listUserWithOrg();


    /**
     * 根据id集合查询用户集
     *
     * @param ids
     * @return
     */
    @Query(value = "SELECT t FROM  DSysUser t WHERE t.id in (:ids) and t.enableFlag = 1")
    List<DSysUser> listByIds(@Param("ids") List<String> ids);

    /**
     * 更改用户密码
     *
     * @param password
     * @param userId
     */
    @Modifying
    @Query("update DSysUser t set t.userPassword = :password  where t.id=:userId and t.enableFlag = 1")
    void updatePassword(@Param("password") String password, @Param("userId") String userId);


    /**
     * 查询所有在用状态用户
     *
     * @return
     */
    @Query("select count(t) from DSysUser t  where t.enableFlag = 1")
    Integer countUser();

    @Query("select user from DSysUser user  inner join DSysUserRole roleuser on user.id=roleuser.userCode  where roleuser.roleId=:id")
    List<DSysUser> getUserByRoleId(@Param("id") String id);

    @Query("select user from DSysUser user  where user.pushId=:pushId and user.enableFlag=1")
    DSysUser getByPushId(@Param("pushId") String pushId);

    @Query("select count(t) from DSysUser t  where t.pushId=:pushId and t.enableFlag = 1")
    Integer countPushId(@Param("pushId") String pushId);

    @Query("select user from DSysUser user  where user.orgId=:orgId and user.enableFlag=1")
    List<DSysUser> getByOrgId(@Param("orgId") String id);

    @Query(value = "select u1.user_login_name as userName ,org.org_name as orgName from d_sys_user u1 inner join d_sys_org org on u1.org_id= org.id where u1.user_phone=:mobile", nativeQuery = true)
    Map<String, Object> getUserByPhone(@Param("mobile") String mobile);
}
